﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ChurchData.Business
{
    public class ImportantDateBO : ChurchDataBusinessBase
    {
        public IEnumerable<ImportantDate> GetActiveDatesByPeoplePK(int peoplePK)
        {
            ChurchDataDataContext oDB = new ChurchDataDataContext(this.GetConnectionString());

            IEnumerable<ImportantDate> result = from id in oDB.ImportantDates
                                                where id.PeopleFK == peoplePK
                                                && id.ActiveInd == true
                                                select id;

            return result;
        }

        public void AddRecord(int peopleFK, int? lkDateTypeFK, DateTime? theDate, string notes)
        {
            ChurchDataDataContext oDB = new ChurchDataDataContext(this.GetConnectionString());

            ImportantDate dr = new ImportantDate();
            dr.PeopleFK = peopleFK;
            dr.LKDateTypeFK = lkDateTypeFK;
            dr.TheDate = theDate;
            dr.Notes = notes;
            dr.ActiveInd = true;
            dr.LastUpdTimestamp = DateTime.Now;
            dr.LastUpdUser = UserInformation.UserName;
            
            oDB.ImportantDates.InsertOnSubmit(dr);

            oDB.SubmitChanges();
        }

        public void UpdateRecord(int importantDatePK, int peopleFK, int? lkDateTypeFK, DateTime? theDate, string notes)
        {
            ChurchDataDataContext oDB = new ChurchDataDataContext(this.GetConnectionString());

            ImportantDate dr = (from id in oDB.ImportantDates
                                where id.ImportantDatePK == importantDatePK
                                select id).First();

            dr.PeopleFK = peopleFK;
            dr.LKDateTypeFK = lkDateTypeFK;
            dr.TheDate = theDate;
            dr.Notes = notes;
            dr.LastUpdTimestamp = DateTime.Now;
            dr.LastUpdUser = UserInformation.UserName;

            oDB.SubmitChanges();
        }

        public void DeleteRecord(int importantDatePK)
        {
            ChurchDataDataContext oDB = new ChurchDataDataContext(this.GetConnectionString());

            ImportantDate dr = (from id in oDB.ImportantDates
                                where id.ImportantDatePK == importantDatePK
                                select id).First();

            dr.ActiveInd = false;
            dr.LastUpdTimestamp = DateTime.Now;
            dr.LastUpdUser = UserInformation.UserName;

            oDB.SubmitChanges();
        }
    }
}
